/*


*/

#include "MsgRounter.h"
#include "Game.h"

MsgRounter::MsgRounter()
{
	//ctor
	m_isInit = false;
}

MsgRounter::~MsgRounter()
{
	//dtor
}

void MsgRounter::ServerRounter(Message* _pMsg)
{
	if(!m_isInit)
	{
		ServerInit();
	}

	eCmd curCmd = _pMsg->m_cmd;
	if(this->m_MsgCallMap.count(curCmd)== 0)
	{
		Debug::Log("ServerRounter error, not found in map: ", (int)curCmd);
		return;
	}

	this->m_MsgCallMap[curCmd](_pMsg);
}

void MsgRounter::ServerInit()
{
	ServerAllRegister();
	m_isInit = true;
}


void OnC2LLoginLogin(Message* _pMsg)
{
	string user = _pMsg->GetString();
	string pwd =_pMsg->GetString();

	//cout<< "C2S_Login_Login, user:"<< user<< " pwd:"<< pwd<< endl;
	int userId = 0;
	int ret = Game::GetInstance().m_Login.Verification(user, pwd, userId);


	Message Msg1(_pMsg->m_fd, eCmd::L2C_Login_Login_Ret);
	Msg1.AddInt(ret);
	Msg1.AddInt(userId);
	Message::Send(&Game::GetInstance().GetSock()->GetMsgQunue(), &Msg1);

}

void OnSYSCheckAnswer(Message* _pMsg)
{
	int retKey = _pMsg->GetInt();
	bool isServer = _pMsg->GetBool();

	// 返回key不对， 不是我们程序 关闭链接
	if(retKey != 8259)
	{
		Debug::Warn("Not our process, shutdown connect.");
		Game::GetInstance().GetSock()->ShutDownAConnect(_pMsg->m_fd);
		return;
	}

	Client* pClient = Game::GetInstance().GetSock()->GetClient(_pMsg->m_fd);
	if(pClient == nullptr)
	{
		Debug::Error("Not found Client in socket");
		return;
	}

	// 链接的socket客户端通过验证
	pClient->m_isPassCheck = true;
	if(isServer)
	{// 是服务器程序
		Game::GetInstance().m_Login.AddServerInfo(pClient);

		// 请求服务器信息
		Message Msg(_pMsg->m_fd, eCmd::L2W_Login_Info);
		Message::Send(&Game::GetInstance().GetSock()->GetMsgQunue(), &Msg);
	}
	else
	{// 是客户端程序
		Game::GetInstance().m_Login.AddClientInfo(pClient);

		// 请求客户端信息
		Message Msg(_pMsg->m_fd, eCmd::L2C_Login_Info);
		Message::Send(&Game::GetInstance().GetSock()->GetMsgQunue(), &Msg);
	}


}

void OnC2LLoginInfo(Message* _pMsg)
{
	string version = _pMsg->GetString();
	string device = _pMsg->GetString();
	string os = _pMsg->GetString();
	string channel = _pMsg->GetString();

	ClientInfo* pInfo = Game::GetInstance().m_Login.GetClientInfo(_pMsg->m_fd);
	if(pInfo == nullptr)
	{
		return;
	}

	pInfo->UpdateVersion(version);
	pInfo->UpdateDevice(device);
	pInfo->UpdateOs(os);
	pInfo->UpdateChannel(channel);

}

void OnW2LLoginInfo(Message* _pMsg)
{
	int id = _pMsg->GetInt();
	string name = _pMsg->GetString();
	eServerState state = (eServerState)_pMsg->GetInt();
	string loginIp = _pMsg->GetString();
	int loginPort = _pMsg->GetInt();


	ServerInfo* pInfo = Game::GetInstance().m_Login.GetServerInfo(_pMsg->m_fd);
	if(pInfo == nullptr)
	{
		return;
	}

	// YU_TODO: 改成方法
	pInfo->m_id = id;
	pInfo->m_name = name;
	pInfo->m_State = state;
	pInfo->m_loginIp = loginIp;
	pInfo->m_loginPort = loginPort;
}

void OnW2LLoginInfoUpdate(Message* _pMsg)
{
	//Debug::Log(" eCmd::W2L_Login_Info_Update");
	int id = _pMsg->GetInt();
	eServerState state = (eServerState)_pMsg->GetInt();
	string loginIp = _pMsg->GetString();
	int loginPort = _pMsg->GetInt();

	ServerInfo* pInfo = Game::GetInstance().m_Login.GetServerInfo(_pMsg->m_fd);
	if(pInfo == nullptr)
	{
		return;
	}

	// YU_TODO: 改成方法
	pInfo->m_State = state;
	pInfo->m_loginIp = loginIp;
	pInfo->m_loginPort = loginPort;
}

void OnC2LLoginServerInfo(Message* _pMsg)
{
	list<ServerInfo> ServerInfo_lst = Game::GetInstance().m_Login.GetServerList();
	int serverCount = ServerInfo_lst.size();

	Message Msg(_pMsg->m_fd, eCmd::L2C_Login_ServerInfo);
	Msg.AddInt(serverCount);

	for(ServerInfo Info : ServerInfo_lst)
	{
		Msg.AddInt(Info.m_id);
		Msg.AddString(Info.m_name);
		Msg.AddInt((int)Info.m_State);
		Msg.AddString(Info.m_loginIp);
		Msg.AddInt(Info.m_loginPort);
	}

	Message::Send(&Game::GetInstance().GetSock()->GetMsgQunue(), &Msg);
}

void OnC2LLoginRegister(Message* _pMsg)
{
	string user = _pMsg->GetString();
	string pwd =_pMsg->GetString();

	//cout<< "C2S_Login_Login, user:"<< user<< " pwd:"<< pwd<< endl;
	int ret = Game::GetInstance().m_Login.Register(user, pwd);

	Message Msg1(_pMsg->m_fd, eCmd::L2C_Login_RegisterRet);
	Msg1.AddInt(ret);
	Message::Send(&Game::GetInstance().GetSock()->GetMsgQunue(), &Msg1);

}

void MsgRounter::ServerAllRegister()
{
	m_MsgCallMap.insert(pair<eCmd, pFunc>(eCmd::C2L_Login_Login, 			OnC2LLoginLogin));
	m_MsgCallMap.insert(pair<eCmd, pFunc>(eCmd::SYS_Check_Answer, 			OnSYSCheckAnswer));
	m_MsgCallMap.insert(pair<eCmd, pFunc>(eCmd::C2L_Login_Info, 			OnC2LLoginInfo));
	m_MsgCallMap.insert(pair<eCmd, pFunc>(eCmd::W2L_Login_Info, 			OnW2LLoginInfo));
	m_MsgCallMap.insert(pair<eCmd, pFunc>(eCmd::W2L_Login_Info_Update, 		OnW2LLoginInfoUpdate));
	m_MsgCallMap.insert(pair<eCmd, pFunc>(eCmd::C2L_Login_ServerInfo, 		OnC2LLoginServerInfo));
	m_MsgCallMap.insert(pair<eCmd, pFunc>(eCmd::C2L_Login_Register, 		OnC2LLoginRegister));
}

